﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Control.Bus;

namespace QuanLyKho
{
    public partial class FrmBO : Form
    {
        private List<BusImportBillDetail> billDetails;

        public List<BusImportBillDetail> BillDetails
        {
            set { billDetails = value; }
            get 
            {
                if (billDetails == null)
                    billDetails = new List<BusImportBillDetail>();
                return billDetails;
            }
        }
       
        
        BusImportBill Bill = new BusImportBill();
        public FrmBO()
        {
            InitializeComponent();
        }
        public FrmBO(BusImportBill _Bill)
        {
            InitializeComponent();
            Bill = _Bill;
        }
        private void btDongY_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow == false)
                {
                    BusProduct p = new BusProduct();
                    p.ProductCode = row.Cells[0].Value.ToString();
                    p.GetProperty();
                    BusImportBillDetail bd = new BusImportBillDetail()
                    {
                        ID = -1,
                        Bill = Bill,
                        Prod = p,
                        Quantity = int.Parse(row.Cells[1].Value.ToString())
                    };
                    BillDetails.Add(bd);
                }
            }
            this.Close();
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
             if(e.Control && e.KeyCode == Keys.V)
             {
                //DATA TABLE THAT WILL STORE PASTED INFORMATION (AS AN INTERMEDIATE STEP)
                //DataTable table_Local = new DataTable();
                //table_Local.Columns.Add("Ma");
                //table_Local.Columns.Add("SoLuong");

                //GET DATA FROM CLIPBOARD
                string s = Clipboard.GetText();
                string[] lines = s.Split(new char[2] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);

                //ITERATION HEADER-CHECK
               
                
                foreach (string line in lines)
                {
                    
                  //SPLIT THE ROW
                  string[] cells = line.Split('\t');
                  bool cellcheck = false;
                  BusProduct p = new BusProduct();
                  p.ProductCode = cells[0];
                  p.GetProperty();
                  if (p.ID != 0)
                  {
                      cellcheck = true;
                  }
                  //IF FIRST PASS, CHECK FOR COLUMN HEADERS
                  //if (IsFirstPass)
                  //{
                  //  foreach (string value in cells)
                  //  { table_Local.Columns.Add(value); }
                  //  //
                  //  IsFirstPass = false;
                  //}
                  //else
                  //{
                  try
                  {
                      
                      //DataRow newRow = table_Local.NewRow();
                      //newRow[0] = cells[0];
                      //newRow[1] = cells.Last();
                      ////newRow.ItemArray = cells;
                      //table_Local.Rows.Add(newRow);
                      string celllast = cells.Last();
                      if (cells.Count() == 1)
                          celllast = "0";
                      dataGridView1.Rows.Add(cells[0], celllast,cellcheck);
                      
                  }
                  catch { MessageBox.Show("Dũ liệu không hợp lệ"); }
                  //}         
                }
                //dataGridView1.AutoGenerateColumns = false;
                //dataGridView1.DataSource = table_Local;
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    row.Selected = false;
                }
                for (int i = dataGridView1.Rows.Count - lines.Count() - 1; i < dataGridView1.Rows.Count - 1; i++)
                {
                    dataGridView1.Rows[i].Selected = true;
                }
              }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btPaste_Click(object sender, EventArgs e)
        {
            string s = Clipboard.GetText();
            string[] lines = s.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            //ITERATION HEADER-CHECK


            foreach (string line in lines)
            {
                string[] cells = line.Split('\t');
                bool cellcheck = false;
                BusProduct p = new BusProduct();
                p.ProductCode = cells[0];
                p.GetProperty();
                if (p.ID != 0)
                {
                    cellcheck = true;
                }
                //IF FIRST PASS, CHECK FOR COLUMN HEADERS
                //if (IsFirstPass)
                //{
                //  foreach (string value in cells)
                //  { table_Local.Columns.Add(value); }
                //  //
                //  IsFirstPass = false;
                //}
                //else
                //{
                try
                {

                    //DataRow newRow = table_Local.NewRow();
                    //newRow[0] = cells[0];
                    //newRow[1] = cells.Last();
                    ////newRow.ItemArray = cells;
                    //table_Local.Rows.Add(newRow);
                    string celllast = cells.Last();
                    if (cells.Count() == 1)
                        celllast = "0";
                    dataGridView1.Rows.Add(cells[0], celllast, cellcheck);

                }
                catch { MessageBox.Show("Dũ liệu không hợp lệ"); }
            }
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                row.Selected = false;
            }
            for (int i = dataGridView1.Rows.Count-lines.Count()-1; i < dataGridView1.Rows.Count-1; i++)
            {
                dataGridView1.Rows[i].Selected = true;
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> rowstodelete = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                if(row.IsNewRow==false)
                    rowstodelete.Add(row);
            }
            foreach (DataGridViewRow row in rowstodelete)
            {
                dataGridView1.Rows.Remove(row);
            }
        }

        private void btok_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow == false)
                {
                    BusProduct p = new BusProduct();
                    p.ProductCode = row.Cells[0].Value.ToString();
                    p.GetProperty();
                    BusImportBillDetail bd = new BusImportBillDetail()
                    {
                        ID = -1,
                        Bill = Bill,
                        Prod = p,
                        Quantity = int.Parse(row.Cells[1].Value.ToString())
                    };
                    BillDetails.Add(bd);
                }
            }
            this.Close();
        }

        private void bth_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
